package com.jack.cms.modular.system.controller;

import org.springframework.stereotype.Controller;

import com.jack.cms.common.controller.BaseController;
import com.jack.cms.common.exception.BizExceptionEnum;
import com.jack.cms.common.exception.BussinessException;
import com.jack.cms.core.support.HttpKit;
import com.jack.cms.core.template.config.ContextConfig;
import com.jack.cms.core.template.engine.SimpleTemplateEngine;
import com.jack.cms.core.template.engine.base.CmsTemplateEngine;
import com.jack.cms.core.util.ToolUtil;
import com.jack.cms.modular.system.api.CodeApi;

@Controller
public class CodeController extends BaseController implements CodeApi {

	private String PREFIX = "/system/code/";

	/**
	 * 跳转到代码生成首页
	 */
	public String index() {
		return PREFIX + "code.html";
	}

	/**
	 * 代码生成
	 */
	public Object add(String moduleName, String bizChName, String bizEnName, String path) {
		String x = "TEST";
		if (x.equals("TEST")) {
			return HttpKit.failed("因为测试，屏蔽生成代码");
		}
		if (ToolUtil.isOneEmpty(bizChName, bizEnName)) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		ContextConfig contextConfig = new ContextConfig();
		contextConfig.setBizChName(bizChName);
		contextConfig.setBizEnName(bizEnName);
		contextConfig.setModuleName(moduleName);
		if (ToolUtil.isNotEmpty(path)) {
			contextConfig.setProjectPath(path);
		}

		CmsTemplateEngine cmsTemplateEngine = new SimpleTemplateEngine();
		cmsTemplateEngine.setContextConfig(contextConfig);
		cmsTemplateEngine.start();

		return SUCCESS_TIP;
	}
}
